// @flow
import React, { Component } from 'react'
import moment from 'moment'
import { AgGridReact } from 'ag-grid-react'
import LinkToJobs from './LinkToJobs'
import styles from './TaskListGrid.css'
import TaskListActions from './TaskListActions'
import { TableResize } from 'ag-grid-addons'

type Props = {
  rowData: Array<Object>,
  refreshTaskList: Function,
  history: Object
}
type States = {
  columnDefs: Array<Object>,
  rowData: Object
}
class TaskListGrid extends Component {
  props: Props
  state: States
  gridOptions: Object
  onGridReady: Function
  TableResize: Function
  goToJobs: Function
  api: Object
  constructor (props: Props) {
    super(props)
    const component = this
    this.state = {
      rowData:{},
      columnDefs:  [
        { headerName: '名称',
          field: 'taskName',
          width: 100,
          cellStyle: {
            padding: '8px',
            'text-align': 'center'
          }
        },
        // { headerName: '模型名称',
        //   field: 'model',
        //   width: 100,
        //   cellStyle: {
        //     padding: '8px',
        //     'text-align': 'center'
        //   },
        //   cellRenderer: (params) => {
        //     if (params.data.model != null) {
        //       return params.data.model.name
        //     } else {
        //       return ''
        //     }
        //   }
        // },
        { headerName: '任务类型',
          field: 'taskType',
          width: 100,
          cellStyle: {
            padding: '8px',
            'text-align': 'center'
          },
          cellRenderer: (params) => {
            return params.data.taskType === 'manual' ? '手动' : '自动'
          }
        },
        { headerName: '部件',
          field: 'devicePart.label',
          width: 50,
          cellStyle: {
            padding: '8px',
            'text-align': 'center'
          }
        },
        // { headerName: '设备类型',
        //   field: 'deviceTypeJson',
        //   width: 100,
        //   cellStyle: {
        //     padding: '8px',
        //     'text-align': 'center'
        //   },
        //   cellRenderer: (params) => {
        //     if (params.data.deviceTypeJson === null || params.data.deviceTypeJson === undefined) {
        //       return ''
        //     } else {
        //       return params.data.deviceTypeJson.label
        //     }
        //   }
        // },
        // { headerName: '客户',
        //   field: 'customer',
        //   width: 100,
        //   cellStyle: {
        //     padding: '8px',
        //     'text-align': 'center'
        //   },
        //   cellRenderer: (params) => {
        //     if (params.data.customer === null || params.data.customer === undefined) {
        //       return ''
        //     } else {
        //       return params.data.customer.label
        //     }
        //   }
        // },
        { headerName: '生效时间',
          field: 'startDate',
          width: 150,
          cellStyle: {
            padding: '8px',
            'text-align': 'center'
          }
          // cellRenderer: (params) => {
          //   return moment.utc(params.data.startDate, 'YYYY-MM-DDTHH:mm:ss.SSSZ').format('YYYY-MM-DD HH:mm:ss')
          // }
        },
        { headerName: '失效时间',
          field: 'endDate',
          width: 150,
          cellStyle: {
            padding: '8px',
            'text-align': 'center'
          }
          // cellRenderer: (params) => {
          //   return moment.utc(params.data.endDate, 'YYYY-MM-DDTHH:mm:ss.SSSZ').format('YYYY-MM-DD HH:mm:ss')
          // }
        },
        { headerName: '使用设备',
          field: 'device',
          width: 150,
          cellStyle: {
            padding: '8px',
            'text-align': 'center'
          },
          cellRenderer: (params) => {
            let deviceNames = '<div>'
            params.data.device.map((d, i) => {
              if (d.deviceName !== null) {
                deviceNames += '[' + d.deviceName + ']'
              }
            })
            deviceNames += '</div>'
            return deviceNames
          }
        },
        { headerName: '作业',
          field: 'model',
          width: 50,
          cellStyle: {
            padding: '8px',
            'text-align': 'center'
          },
          cellRendererParams: {
            goToJobs: component.goToJobs.bind(this)
          },
          cellRendererFramework: LinkToJobs
        },
        {
          headerName: '操作',
          minWidth: 200,
          cellRendererParams: {
            history: this.props.history,
            refreshTaskList: this.props.refreshTaskList
          },
          cellRendererFramework: TaskListActions,
          cellStyle: {
            padding: '8px',
            'text-align': 'left'
          }
        }
      ]
    }
    this.gridOptions = {
      localeText: {
        loadingOoo: '数据加载中...',
        noRowsToShow: '暂无数据',
        filterOoo: '搜索当前页面',
        equals: '等于',
        notEqual: '不等于',
        contains: '包含',
        startsWith: '开始于',
        endsWith: '结束于',
        copy: '复制',
        paste: '粘贴',
        copyWithHeaders: '带表头复制',
        toolPanel: '工具栏',
        export: '导出',
        csvExport: '导出CSV',
        excelExport: '导出Excel'
      },
      enableSorting: true,
      enableFilter: true,
      rowSelection: 'single',
      rowDeselection: true,
      enableColResize: true
    }
    this.onGridReady = this.onGridReady.bind(this)
    this.TableResize = TableResize.bind(this)
    this.goToJobs = this.goToJobs.bind(this)
  }

  onGridReady (params: Object) {
    this.api = params.api
    this.api.sizeColumnsToFit()
    this.TableResize()
  }
  goToJobs (data: Object) {
    this.props.history.push({
      pathname: `/analysis/jobs`,
      state: {
        taskData: data
      }
    })
  }
  componentWillReceiveProps (newProps: Object) {
    this.setState({ rowData: newProps })
  }
  componentWillMount () {
    this.setState({ rowData: this.props })
  }
  render () {
    const { columnDefs, rowData } = this.state
    return (
      <div className={styles['grid'] + ' ag-fresh'}>
        <AgGridReact {...{
          gridOptions: this.gridOptions,
          rowHeight: '34',
          headerHeight: '28',
          columnDefs: columnDefs,
          rowData: rowData.rowData,
          onGridReady: this.onGridReady
        }}
          />
      </div>
    )
  }
}

export default TaskListGrid
